package com.example.camerafacedetect.faceframe;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.media.FaceDetector;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import com.google.mlkit.vision.face.Face;

import java.util.List;

/**
 * Created by yubo on 2015/9/5.
 * 画人脸区域的View
 */
public class FaceView extends androidx.appcompat.widget.AppCompatImageView {
    private Paint paint;
    private Bitmap bitmap;

    public FaceView(Context context) {
        super(context);
        init(context);
    }

    public FaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public FaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);//设置话出的是空心方框而不是实心方块
    }

    private Bitmap black_bitmap;

    public void setFaces(Bitmap bitmap) {
        // 创建一个黑色图片，用于结束的时候黑屏
        if(black_bitmap == null){
            black_bitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.RGB_565);
            black_bitmap.eraseColor(Color.BLACK);//填充颜色
            black_bitmap = ImageUtils.rotateBitmap(black_bitmap, 270);
        }
        bitmap = ImageUtils.rotateBitmap(bitmap, 270);
        setImageBitmap(bitmap);
        // 将会触发onDraw事件
        invalidate();
    }

    public void black_screen(){
        setImageBitmap(black_bitmap);
    }

    private float scaleRate = 1.0f;

    public void setScaleRate(float rate) {
        this.scaleRate = rate;
    }

    /** 清除数据 */
    public void clear(){
        postInvalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
}
